<?php

    session_start('studycheat_net');
	header("content-type: text/xml");

    // database connection
    include( '../connect.php' );
    
    // return as XML document
    $doc = new DOMDocument('1.0');
    $node = $doc->createElement('markers');
    $parnode = $doc->appendChild($node);

    // return studying students in defined radius
    if (!empty($_GET['radius'])) {
        $_GET['lat']    = mysql_escape_string((float) $_GET['lat']);
        $_GET['lng']    = mysql_escape_string((float) $_GET['lng']);
        $_GET['radius'] = mysql_escape_string((int) $_GET['radius']);
        $_GET['course'] = (empty($_GET['course'])) ? '' :  'AND course_id = ' . mysql_escape_string((string) $_GET['course']);

        $query = sprintf("SELECT * ( 6371 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM course_online WHERE 1=1 %s HAVING distance < '%s' ORDER BY distance",
                    $_GET['lat'],
                    $_GET['lng'],
                    $_GET['lat'],
                    $_GET['course'],
                    $_GET['radius']
                 );

        $res = mysql_query($query);
    }
    
    if (mysql_num_rows($res) > 0) {
        while ($row = mysql_fetch_array($res)) {

            // pridam uzol do XML
            $node = $doc->createElement("marker");
            $newnode = $parnode->appendChild($node);
            
            $newnode->setAttribute("id", $row['id']);
            $newnode->setAttribute("lat", $row['lat']);
            $newnode->setAttribute("lng", $row['lng']);
            $newnode->setAttribute("html", $row['description']);
            $newnode->setAttribute("title", $row['title']);
            $newnode->setAttribute("course", $row['course']);
        }
    } 

    echo $doc->saveXML();

?>
 